package org.openpanodroid.panoutils.android;

import junit.framework.Assert;
import android.graphics.Bitmap;

public class CubicPanoNative {
	public static final String LOG_TAG = CubicPanoNative.class.getSimpleName();

	public enum TextureFaces {
		front, back, top, bottom, left, right
	};

	private Bitmap front, back, top, bottom, left, right;

	static {
		System.loadLibrary("cubicpano-jni");
	}

	public CubicPanoNative(Bitmap front, Bitmap back, Bitmap top, Bitmap bottom, Bitmap left, Bitmap right) {
		Assert.assertFalse(front == null);
		Assert.assertFalse(back == null);
		Assert.assertFalse(top == null);
		Assert.assertFalse(bottom == null);
		Assert.assertFalse(left == null);
		Assert.assertFalse(right == null);

		Assert.assertTrue(front.getWidth() == front.getHeight());
		Assert.assertTrue(back.getWidth() == back.getHeight());
		Assert.assertTrue(left.getWidth() == left.getHeight());
		Assert.assertTrue(right.getWidth() == right.getHeight());
		Assert.assertTrue(top.getWidth() == top.getHeight());
		Assert.assertTrue(bottom.getWidth() == bottom.getHeight());

		Assert.assertTrue(front.getWidth() == back.getWidth());
		Assert.assertTrue(front.getWidth() == left.getWidth());
		Assert.assertTrue(front.getWidth() == right.getWidth());
		Assert.assertTrue(front.getWidth() == top.getWidth());
		Assert.assertTrue(front.getWidth() == bottom.getWidth());

		this.front = front;
		this.back = back;
		this.top = top;
		this.bottom = bottom;
		this.left = left;
		this.right = right;
	}

	public Bitmap getFace(TextureFaces face) {
		Bitmap bmp = null;

		switch (face) {
		case front:
			bmp = front;
			break;
		case back:
			bmp = back;
			break;
		case left:
			bmp = left;
			break;
		case right:
			bmp = right;
			break;
		case top:
			bmp = top;
			break;
		case bottom:
			bmp = bottom;
			break;
		default:
			Assert.fail();
		}
		return bmp;
	}

	public static Bitmap getCubeSide(Bitmap pano, TextureFaces face, int widthTexture) {
		if (pano.getConfig() != Bitmap.Config.ARGB_8888) {
			return null;
		}

		Bitmap faceBmp = Bitmap.createBitmap(widthTexture, widthTexture, Bitmap.Config.ARGB_8888);

		calculateCubeSide(pano, faceBmp, face.ordinal());

		return faceBmp;
	}

	// implemented by libcubicano-jni.so
	private static native void calculateCubeSide(Bitmap panoBmp, Bitmap faceBmp, int faceNo);

}